<?php 
$bg_imgs = scandir(TH_DIR . '/' . basename(dirname(__FILE__)) . '/skin/images/__bg/');
$idx = rand() % count($bg_imgs);
$idx = max($idx, 3);
if (isset($_SESSION['admin_user'])) { 
  $user = json_decode($_SESSION['admin_user']);
} else {
  $user = new stdClass();
  $user->role = '';
}
if ($user->role == UT_MARKETING && ($app_data->contents[$app_data->query]->id != 65 && $app_data->contents[$app_data->query]->id != 4 && $app_data->contents[$app_data->query]->id != 1)) { 
  return;
}
if ($user->role == UT_STAFF && ($app_data->contents[$app_data->query]->id == 63 || $app_data->contents[$app_data->query]->id == 64)) { 
  return;
}
if ($user->role == UT_FINANCE && ($app_data->contents[$app_data->query]->id == 63 || $app_data->contents[$app_data->query]->id == 64)) { 
  return;
}
?>
<!DOCTYPE html>
<html>
  <head>
    <title><?php echo $app_data->sys->name; ?></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="icon" type="image/ico" href="<?php echo TH_DIR . basename(dirname(__FILE__)); ?>/skin/images/favicon.ico"/>
    <?php include '../include/common.inc.php'; ?>
    <style type="text/css"><?php echo $app_data->contents[$app_data->query]->css; ?></style>
    <?php 
      if (!isset($app_data->contents[$app_data->query]->is_iframe) || $app_data->contents[$app_data->query]->is_iframe == 0) {
        $hdfile = str_replace('.php', '', $app_data->contents[$app_data->query]->html) . '.hd.php';
        if (file_exists($hdfile)) {
          include $hdfile;
        }
      }
    ?>
    <link rel="stylesheet" href="<?php echo TH_DIR . basename(dirname(__FILE__)); ?>/jMenu/css/jmenu.css" />
    <script type="text/javascript" src="<?php echo TH_DIR . basename(dirname(__FILE__)); ?>/jMenu/js/jMenu.jquery.js"></script>
    <style type="text/css">
      body { background: url('<?php echo TH_DIR . basename(dirname(__FILE__)) . '/skin/images/__bg/' . $bg_imgs[$idx]; ?>') no-repeat center top; min-height: 500px; }
    </style>
  </head>

  <body class="pure-skin-mine">
    <div class="cs_header">
      <?php if ($app_data->sys->login_required && isset($app_data->is_authorised) && $app_data->is_authorised == 1) { ?>
      <div class="cs_logout" onclick="location.href=document.URL.replace('#', '') + ((document.URL.indexOf('?') == -1) ? '?logout=1' : '&logout=1'); return false;"><a>Logout</a></div>
      <div class="cs_profile" onclick="Url.redirect(____redmd, ____sys + '.php?i=65');"><a><?php echo $user->account; ?></a></div>
      <?php } ?>
      <div class="cs_menubar"></div>
    </div>
    <div class="cs_navbar ui-widget-header ui-corner-all" style="display: none;"></div>
    <?php if ($app_data->param_id != $app_data->__login_id) { ?>
    <div class="cs_body_wraper">
      <table class="cs_lrtable" cellpadding="0" cellspacing="0">
        <tr>
          <td class="cs_leftbar_wraper">
            <div id="cs_leftbar_objs" class="ui-widget-content ui-corner-all">&nbsp;</div>
            <div id="cs_leftbar" class="ui-widget-content ui-corner-all">&nbsp;</div>
          </td>
          <td>
            <div class="ui-widget-content ui-corner-all cs_body" <?php if ($app_data->contents[$app_data->query]->html == '') echo 'style="display: none;"'; ?>>
              <?php
                if (isset($app_data->contents[$app_data->query]->__iframe) && $app_data->contents[$app_data->query]->__iframe == 1) {
                  echo $app_data->contents[$app_data->query]->html;
                } else {
                  if (file_exists($app_data->contents[$app_data->query]->html)) {
                    include $app_data->contents[$app_data->query]->html;
                  }
                }
              ?>
            </div>
          </td>
        </tr>
      </table>
    </div>
    <?php } else { ?>
    <div class="ui-widget-content ui-corner-all cs_body" style="background: none; border: none;">
      <?php 
        if (isset($app_data->contents[$app_data->query]->__iframe) && $app_data->contents[$app_data->query]->__iframe == 1) {
          echo $app_data->contents[$app_data->query]->html;
        } else {
          if (file_exists($app_data->contents[$app_data->query]->html)) {
            include $app_data->contents[$app_data->query]->html;
          }
        }
      ?>
    </div>
    <?php } ?>
    <script type="text/javascript">
      /*<![CDATA[*/
      var ____user = JSON.parse('<?php echo addslashes(json_encode($user)); ?>');
      
      function ____menubar(id, root) {
        var mmid = ____mmid(root, id);
        $(".cs_menubar").empty();
        $('.cs_menubar').append('<ul id="j_menu"></ul>');
        ____menu(root, $('#j_menu'), mmid);
        $("#j_menu").jMenu({
          openClick : false,
          ulWidth :'auto',
          TimeBeforeOpening : 100,
          TimeBeforeClosing : 11,
          animatedText : false,
          paddingLeft: 1,
          effects : {
              effectSpeedOpen : 15,
              effectSpeedClose : 150,
              effectTypeOpen : 'slide',
              effectTypeClose : 'slide',
              effectOpen : 'swing',
              effectClose : 'swing'
          }
        });
      };
      
      function ____menu(node, elm, mmid) {
        for (var idx in node.__c) {
          var itm = node.__c[idx];
          if (____user.role == 'Marketing' && itm.id != 4) {
            continue;
          }
          if (____user.role == 'Mgmt' && itm.id == 63) {
            continue;
          }
          if (____user.role == 'Finance' && (itm.id == 2 || itm.id == 63)) {
            continue;
          }
          elm.append('<li' + ((mmid == itm.id) ? ' class="cs_hi"' : '') + ' value="' + itm.id +'">' +
                       '<a href="' + ____sys + '.php?i=' + itm.id + '" onclick="return Url.redirect(' + ____redmd + ', \'' + ____sys + '.php?i=' + itm.id + '\');">' + ((itm.icon == '') ? '' : '<i class="fa fa-fw fa-lg">' + itm.icon + '</i>') + '&nbsp;' + itm.name + '</a>' +
                       ((itm.__c.length > 0) ? '<ul id="pgul_' + itm.id + '"></ul>' : '') + 
                     '</li>');
          if (itm.__c.length > 0) {
            ____menu(itm, $('#pgul_' + itm.id), mmid);
          }
        }
      };
      /*]]>*/
    </script>
    <?php include '../include/inc.theme.js.php'; ?>
  </body>
</html>